package com.aat.dxfy.shiro;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory;

public class Test03 {

	public static void main(String[] args) {
		// 1.加载shiro安全管理
		Factory<SecurityManager> fac = new IniSecurityManagerFactory("classpath:dxfyShiro3.ini");
		// 2.安全管理开始创建
		SecurityManager securityManager = fac.getInstance();
		// 3.安全工具里放入安全管理器
		SecurityUtils.setSecurityManager(securityManager);
		// 4.安全工具取得用户或者当前的执行者
		Subject currentUser = SecurityUtils.getSubject();

		// 6.当前用户有没有被验证是可用的，如果没有证明或者验证 ，用户密码器执行登录验证
		if (!currentUser.isAuthenticated()) {
			UsernamePasswordToken token = new UsernamePasswordToken("admin", "123", true);
			try {
				currentUser.login(token);
			} catch (UnknownAccountException e) {
				System.out.println("没有这个用户！");
			} catch (IncorrectCredentialsException e) {
				System.out.println("密码错误！");
			} catch (LockedAccountException e) {
				System.out.println("账户被锁定！");
			} catch (AuthenticationException e) {
				System.out.println("未知的原因！");
			}
			System.out.println("User:" + currentUser.getPrincipal());
			
			System.out.println("------------------");
			// 8.测试角色
			if (currentUser.hasRole("0")) {
				System.out.println("拥有0角色");
			} else {
				System.out.println("对不起，没有0角色。");
			}
			if (currentUser.hasRole("1")) {
				System.out.println("拥有1角色");
			} else {
				System.out.println("没有1角色。");
			}
			
			System.out.println("------------------");
			// 9,测试权限
			System.out.println(currentUser.toString());
			String a="*",b="user",c = "edit";
			List<String> list=zuhe(a, b, c);
			for (String s : list) {
				if (currentUser.isPermitted(s)) {
					System.out.println("拥有  "+s);
				} else {
					System.out.println("没有  "+s);
				}
			}
			System.out.println("------------------");
			// 11.用户完事后，注销
			currentUser.logout();
			System.out.println("host:" + token.getHost());
			System.out.println("注销退出。");
		}

		System.exit(0);
	}
	
	public static List<String> zuhe(String a,String b, String c){
		List<String> list=new ArrayList<String>();
		list.add(a);
		list.add(b);
		list.add(c);
		
		list.add(a+":"+a);
		list.add(a+":"+b);
		list.add(a+":"+c);
		
		list.add(b+":"+a);
		list.add(b+":"+b);
		list.add(b+":"+c);
		
		list.add(c+":"+a);
		list.add(c+":"+b);
		list.add(c+":"+c);
		
		list.add(a+":"+a+":"+a);
		list.add(a+":"+b+":"+a);
		list.add(a+":"+c+":"+a);
		
		list.add(a+":"+a+":"+b);
		list.add(a+":"+b+":"+b);
		list.add(a+":"+c+":"+b);
		
		list.add(a+":"+a+":"+c);
		list.add(a+":"+b+":"+c);
		list.add(a+":"+c+":"+c);
		
		list.add(b+":"+a+":"+a);
		list.add(b+":"+b+":"+a);
		list.add(b+":"+c+":"+a);
		
		list.add(b+":"+a+":"+b);
		list.add(b+":"+b+":"+b);
		list.add(b+":"+c+":"+b);
		
		list.add(b+":"+a+":"+c);
		list.add(b+":"+b+":"+c);
		list.add(b+":"+c+":"+c);
		
		list.add(c+":"+a+":"+a);
		list.add(c+":"+b+":"+a);
		list.add(c+":"+c+":"+a);
		
		list.add(c+":"+a+":"+b);
		list.add(c+":"+b+":"+b);
		list.add(c+":"+c+":"+b);
		
		list.add(c+":"+a+":"+c);
		list.add(c+":"+b+":"+c);
		list.add(c+":"+c+":"+c);
		return list;
	}
	
	void test(){
		/*
		[users]
		admin=123,9
		[roles]
		0=*
		1=*:*:edit
		2=*:*:read
		3=*:user:edit
		4=*:user:read
		5=ress:user:edit
		6=ress:user:read
		7=user:*
		8=user:edit
		9=*:user:*
		 * 
		 */
	}

}
